package it.unibo.iss.domoticaContact;

import it.unibo.contact.platform.InteractPolicy;
import it.unibo.contact.platform.PlatformExpert;
import it.unibo.is.interfaces.*;
import it.unibo.is.interfaces.platforms.*;
import it.unibo.iss.domotica.edControlling.EDController;

import java.util.Vector;
	

public abstract class  EDControllerSupport extends Subject{
	/* -------------------------------------
	* Local state of the subject
	* --------------------------------------
	*/
	
	protected String M = "someContent";	
	protected int lastMsgNum = 0; 
	private EDController obj = null; //The user must define EdController
//	private  IOutputView view;
	protected Vector<IMessage> inpList;
	
	/* -------------------------------------
	* OPERATIONS
	* --------------------------------------
	*/
	
	
	
	protected void edControllerForward_listEdSuperflui_usrcmdContact() throws Exception{
		IOpOut support = PlatformExpert.findOutSupport("usrcmdContact","listEdSuperflui","edController");
		support.forward( "edController", "listEdSuperflui" , M, "usrcmdContact", InteractPolicy.nopolicy() );
//		showMsg( " has forwarded ... " + "listEdSuperflui  to " + " usrcmdContact" );
	}
	
	protected void evalRequest_listEd(IMessageAndContext mCtx) throws Exception{
 		//showMessage( " received", mCtx.getReceivedMessage() );		
 		//TODO elaborate the message
 		M= "aedControllerlistEd" ;
	}
	
	protected void replyToGrant_listEd(IMessageAndContext mCtx) throws Exception{
 		mCtx.replyToCaller("listEd answer=" + M );		
	}
	
	protected IMessageAndContext edControllerGrant_listEd() throws Exception{
	IOpIn support = PlatformExpert.findInSupport("edController","listEd",true );
	IMessageAndContext m = support.grant( "edController" ,  "listEd", InteractPolicy.nopolicy()   );
		//showMessage( "acquired", m.getReceivedMessage() ) ;
		return m;
	}
	
	protected void evalRequest_powerOn(IMessageAndContext mCtx) throws Exception{
 		//showMessage( " received", mCtx.getReceivedMessage() );		
 		//TODO elaborate the message
 		M= "aedControllerpowerOn" ;
	}
	
	protected void replyToGrant_powerOn(IMessageAndContext mCtx) throws Exception{
 		mCtx.replyToCaller("powerOn answer=" + M );		
	}
	
	protected IMessageAndContext edControllerGrant_powerOn() throws Exception{
	IOpIn support = PlatformExpert.findInSupport("edController","powerOn",true );
	IMessageAndContext m = support.grant( "edController" ,  "powerOn", InteractPolicy.nopolicy()   );
		//showMessage( "acquired", m.getReceivedMessage() ) ;
		return m;
	}
	
	protected void evalRequest_powerOff(IMessageAndContext mCtx) throws Exception{
 		//showMessage( " received", mCtx.getReceivedMessage() );		
 		//TODO elaborate the message
 		M= "aedControllerpowerOff" ;
	}
	
	protected void replyToGrant_powerOff(IMessageAndContext mCtx) throws Exception{
 		mCtx.replyToCaller("powerOff answer=" + M );		
	}
	
	protected IMessageAndContext edControllerGrant_powerOff() throws Exception{
	IOpIn support = PlatformExpert.findInSupport("edController","powerOff",true );
	IMessageAndContext m = support.grant( "edController" ,  "powerOff", InteractPolicy.nopolicy()   );
		//showMessage( "acquired", m.getReceivedMessage() ) ;
		return m;
	}
	
	protected IMessage edControllerServe_abortPowerOn() throws Exception{
		IOpIn support = PlatformExpert.findInSupport("edController","abortPowerOn",false );
		IMessage m =  support.serve( "edController", "abortPowerOn", InteractPolicy.nopolicy()  ); 
		//showMessage( "serving" , m );
		return m;
	}
		
	/* -------------------------------------
	* Local body of the subject
	* --------------------------------------
	*/
	/*
	* Executes the job related to a specific message id
	*/
 	protected void elab(String msgId) throws Exception{
		showMsg("elab "  +  msgId );
	if( msgId.equals("listEd")){
		IMessageAndContext m = edControllerGrant_listEd();
	 	evalRequest_listEd( m  );
	 	replyToGrant_listEd( m );
 	}
	if( msgId.equals("powerOn")){
		IMessageAndContext m = edControllerGrant_powerOn();
	 	evalRequest_powerOn( m  );
	 	replyToGrant_powerOn( m );
 	}
	if( msgId.equals("powerOff")){
		IMessageAndContext m = edControllerGrant_powerOff();
	 	evalRequest_powerOff( m  );
	 	replyToGrant_powerOff( m );
 	}
	if( msgId.equals("abortPowerOn")){
		IMessage m = edControllerServe_abortPowerOn();
		//showMessage( m );
	}
	}//elab	
 
	protected abstract void doJob() throws Exception;	/*{
	[it.unibo.is.contact.impl.grantRequestImpl@848ecc, it.unibo.is.contact.impl.grantRequestImpl@164cbde, it.unibo.is.contact.impl.grantRequestImpl@19b4748, it.unibo.is.contact.impl.serveDispatchImpl@5e8d7d]
	[]
 	 //	INPUT
   	prepareInput();
   	IMessage mm = selectOneInput(lastMsgNum, inpList);
   	elab(mm.msgId());
	//	OUTPUT
	{	 edControllerForward_listEdSuperflui_usrcmdContact() ;
	}
 	}  */	


protected Vector<IMessage> prepareInput() throws Exception {
showMsg(" ----------- prepareInput ----------  "   );
  
//All input is put in the shared space 4
Vector<IMessage> inpList = new Vector<IMessage>();
		PlatformExpert.findInSupport("edController","listEd",true );
		PlatformExpert.addInput(inpList, "edController", "listEd", InteractPolicy.nopolicy(), true);
		PlatformExpert.findInSupport("edController","powerOn",true );
		PlatformExpert.addInput(inpList, "edController", "powerOn", InteractPolicy.nopolicy(), true);
		PlatformExpert.findInSupport("edController","powerOff",true );
		PlatformExpert.addInput(inpList, "edController", "powerOff", InteractPolicy.nopolicy(), true);
		PlatformExpert.findInSupport("edController","abortPowerOn",false );
		PlatformExpert.addInput(inpList, "edController", "abortPowerOn", InteractPolicy.nopolicy(), false);


	return inpList;
}	 	
	


	

	
	/* -------------------------------------
	* Termination
	* --------------------------------------
	*/
	
protected void terminate() throws Exception{
	edControllerForward_listEdSuperflui_usrcmdContactEnd();

	edControllerGrant_listEdEnd();
	edControllerGrant_powerOnEnd();
	edControllerGrant_powerOffEnd();
	edControllerServe_abortPowerOnEnd();
	
//showMsg("  *** TERMINATED *** ");
}
//Teminate operations

	
	protected void edControllerForward_listEdSuperflui_usrcmdContactEnd() throws Exception{
		IOpOut support = PlatformExpert.findOutSupport("usrcmdContact","listEdSuperflui","edController");
		support.terminate();
	}
	
 	protected void edControllerGrant_listEdEnd() throws Exception{
		IOpIn support = PlatformExpert.findInSupport("edController","listEd",true );
		support.terminate();
	}
 	protected void edControllerGrant_powerOnEnd() throws Exception{
		IOpIn support = PlatformExpert.findInSupport("edController","powerOn",true );
		support.terminate();
	}
 	protected void edControllerGrant_powerOffEnd() throws Exception{
		IOpIn support = PlatformExpert.findInSupport("edController","powerOff",true );
		support.terminate();
	}
	
	protected void edControllerServe_abortPowerOnEnd() throws Exception{
		IOpIn support = PlatformExpert.findInSupport("edController","abortPowerOn",false );
		support.terminate();
	}
	

}
